package com.gitee.huanminabc.db_freedom.config;



import com.gitee.huanminabc.db_freedom.action.OperationRecordAction;
import com.gitee.huanminabc.db_freedom.action.SqlPrintRecordAction;
import com.gitee.huanminabc.db_freedom.proxy.*;
import com.gitee.huanminabc.db_freedom.base.SqlHelper;
import com.gitee.huanminabc.db_freedom.wrapper.SqlWrapperHelper;
import com.gitee.huanminabc.utils_tools.spring_base.utils.SpringContextUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@DependsOn("springContextUtil")
@Configuration
@ComponentScan("com.gitee.huanminabc.db_freedom")
public class MybatisFreedomConfig {
    public static CoreActionProxy actionProxy = new CoreActionProxy();

    public static  List<ActionProxy> actionProxies=new ArrayList<ActionProxy>(){{
        add(new OperationRecordAction());
        add(new SqlPrintRecordAction());
    }};

    public MybatisFreedomConfig() {
        actionProxies.add(new OperationRecordAction());
    }

    @Bean
    @DependsOn("sqlHelper")
    public SqlWrapperHelper sqlWrapperHelper(SqlHelper sqlHelper){
        ProxyCglibFactory proxyCglibFactory = new ProxyCglibFactory(MybatisFreedomConfig.actionProxy, sqlHelper);
        SqlHelper object = ProxyCglibInstance.getObject(sqlHelper, proxyCglibFactory);
        return new SqlWrapperHelper(object);
    }


    @PostConstruct //初始化
    public void init(){
        for (String beanName : SpringContextUtil.getBeanNamesForType(ActionProxy.class)) {
            actionProxies.add(SpringContextUtil.getBean(beanName, ActionProxy.class));
        }
    }
}
